﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace TerenXNA
{
    class ScreenMenuButton
    {
        public Rectangle buttonRectangle;
        private String buttonText;
        private Vector2 buttonTextLocation;
        private bool pressed = false;
        private bool lastMouseButtonPressed = false;
        private SpriteBatch spriteBatch;
        private Texture2D solidTexture;

        public delegate void ClickActionDelegate();
        public ClickActionDelegate clickActionDelegate;
        private Color buttonBackgroundColor;


        public ScreenMenuButton(int x, int y, String text, SpriteBatch spritebatch, Texture2D texture)
        {
            this.buttonText = text;
            //this.buttonTextLocation = new Vector2(x + 25, y + 15);

            Vector2 stringMeasureVector = ScreenMenu.spriteFont.MeasureString(text);

            this.buttonTextLocation = new Vector2(x+(ScreenMenu.buttonWidth - stringMeasureVector.X) / 2, y+(ScreenMenu.buttonHeight - stringMeasureVector.Y) / 2); 

            this.buttonRectangle = new Rectangle(x, y, ScreenMenu.buttonWidth, ScreenMenu.buttonHeight);
            this.spriteBatch = spritebatch;
            this.solidTexture = texture;

            this.buttonBackgroundColor = Color.Orange;

        }

        public void MouseUpdate(MouseState mouseState)
        {
            if (buttonRectangle.Contains(new Point(mouseState.X, mouseState.Y)))
            {
                buttonBackgroundColor = Color.Red;

                if (mouseState.LeftButton == ButtonState.Pressed && !lastMouseButtonPressed)
                {
                    //this.buttonText = "Pressed";
                    buttonBackgroundColor = Color.Purple;
                    lastMouseButtonPressed = !lastMouseButtonPressed;
                }
                else if (mouseState.LeftButton == ButtonState.Pressed && lastMouseButtonPressed)
                {
                    //this.buttonText = "Pressed";
                    buttonBackgroundColor = Color.Purple;
                }
                else if (mouseState.LeftButton == ButtonState.Released && lastMouseButtonPressed)
                {
                    //this.buttonText = "Released";

                    //ScreenMenu.actualCameraMode = ScreenMenu.SwitchToNextCameraMode();

                    this.clickActionDelegate();

                    buttonBackgroundColor = Color.Red;
                    lastMouseButtonPressed = !lastMouseButtonPressed;
                }
            }
            else
            {
                buttonBackgroundColor = Color.Orange;
            }
        }

        public void Draw()
        {
            spriteBatch.Draw(solidTexture, this.buttonRectangle, buttonBackgroundColor);
            spriteBatch.DrawString(ScreenMenu.spriteFont, buttonText, buttonTextLocation, Color.Black);
        }

    }
}
